package com.tj.jinzao.fragment;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tj.jinzao.R;
import com.tj.jinzao.adapter.OccupationListItemAdapter;
import com.tj.jinzao.base.BaseDataListFragment;
import com.tj.jinzao.callback.IJobView;
import com.tj.jinzao.model.Occupation;
import com.tj.jinzao.presenter.LabelOccupationPresenter;
import com.tj.jinzao.utils.Utils;
import com.tj.jinzao.widgets.EmptyView;

import java.util.List;

import butterknife.BindView;

/**
 * 社区文章列表
 * Created by wjy on 2019/1/1.
 */
public class ChooseOccupationFragment extends BaseDataListFragment<List<Occupation.ItemsBean>, LabelOccupationPresenter, Occupation.ItemsBean> implements IJobView<List<Occupation.ItemsBean>> {

    @BindView(R.id.ll_label_layout)
    LinearLayout mLabelLayout;
    private int mSelectedIndex;
    private View mCurrentView;

    public static ChooseOccupationFragment newInstance() {
        ChooseOccupationFragment fragment = new ChooseOccupationFragment();
        if (fragment != null) {
            Bundle args = new Bundle(3);
            args.putString(ARG_TAG_ID, String.valueOf(1));
            args.putInt(ARG_LOAD_ID, 1);
            args.putInt(ARG_LAYOUT_RES_ID, R.layout.fragment_choose_occupation);
            fragment.setArguments(args);
        }
        return fragment;
    }

    @Override
    public void onRequestNoData(String message) {
        setEmptyDataView();
    }

    @Override
    protected LabelOccupationPresenter createPresenter() {
        return new LabelOccupationPresenter(this);
    }

    @Override
    protected void createView(LayoutInflater inflater, View view) {
    }

    @Override
    protected RecyclerView.Adapter createAdapter() {
        return new OccupationListItemAdapter(mContext, mData);
    }

    @Override
    protected void requestData() {
        mPresenter.getOccupations();
    }

    @Override
    protected void setEmptyDataView() {
        mEmptyView.setVisibility(View.VISIBLE);
        mEmptyView.setState(EmptyView.State.NO_DATA, "没有内容哦");
    }

    @Override
    protected boolean resolveData(List<Occupation.ItemsBean> itemsBeans) {
        mData.clear();
        mData.addAll(itemsBeans);
        showListView();
        return false;
    }
    private String title="医生";

    @Override
    public void onClick(View v) {
        super.onClick(v);
        int position = (Integer) v.getTag(R.id.position);
        if (position != mSelectedIndex) {
            mSelectedIndex = position;
            mCurrentView.setBackgroundResource(R.color.activity_bg);
            v.setBackgroundResource(R.drawable.bg_occupation_label);
            mCurrentView = v;
            mPresenter.getOccupationListByLabel(mSelectedIndex);
        }
        if(list!=null){
            title=list.get(mSelectedIndex).getName();
        }
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void itemClicked(int position, Occupation.ItemsBean data) {
        Intent to=new Intent();
        to.putExtra("title",title);
        to.putExtra("name",data.getItemName());
        getActivity().setResult(Activity.RESULT_OK,to);
        getActivity().finish();

    }
    private List<Occupation> list;

    @Override
    public void onLabelList(List<Occupation> list) {
        this.list=list;
        int index = 0;
        int padding = Utils.dpToPx(10, getResources());
        for (Occupation bean : list) {
            TextView textView = new TextView(mContext);
            textView.setTextColor(Color.parseColor("#535671"));
            textView.setTextSize(14);
            textView.setTag(R.id.position, index);
            textView.setText(bean.getName());
            textView.setPadding(0, padding, 0, padding);
            textView.setBackgroundResource(R.color.activity_bg);
            textView.setGravity(Gravity.CENTER);
            textView.setOnClickListener(this);
            mLabelLayout.addView(textView);
            if (mSelectedIndex == index) {
                mCurrentView = textView;
                mCurrentView.setBackgroundResource(R.drawable.bg_occupation_label);
                mPresenter.getOccupationListByLabel(mSelectedIndex);
            }
            index++;
        }
    }
}
